
class Person {
 
    String name
    Integer age
    def increaseAge(Integer years) {
        this.name += years
    }
 
    def invokeMethod(String name, Object args) {
        return "the methos is ${name}, the params is ${args}"
    }
 
    def methodMissing(String name, Object args){
        return "the methos is ${name} is missing"
    }
}

// ================== 1. 输出JSON字符串
import groovy.json.JsonOutput
def strinify(){
    
    def list = [new Person(name: 'John', age: 25),
                new Person(name: 'Major', age: 26)]
    println JsonOutput.toJson(list)
    //带格式的json字符串
    def json = JsonOutput.toJson(list)
    println JsonOutput.prettyPrint(json)
}




// ================== 2. json转换成对象
import groovy.json.JsonSlurper
def parse(){
    def jsonSlpuer = new JsonSlurper()
    println jsonSlpuer.parse(new ByteArrayInputStream(json.getBytes()))

    def versionObjs = jsonSlpuer.parse(new java.io.ByteArrayInputStream("{\"name\":1}".getBytes()))
    println versionObjs['name']
    println versionObjs.name

    versionObjs = jsonSlpuer.parse(new File(System.getProperty("user.dir") + "/json.json"))
    println versionObjs.totalCount
}

def readJsonFile(){
    jsonText = new File("D:/Projects/BDCDJ/nfqq/doc/jenkins-ansible-config.json").text
    println groovy.json.JsonOutput.toJson(new JsonSlurper().parse(new ByteArrayInputStream(jsonText.getBytes())))

}


readJsonFile()

